/*
 * main.c
 *
 * Created: 08.02.2013 11:43:52
 *  Author: saenko
 */ 


#include <avr/io.h>
#include <stdio.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/delay.h>


#include "uart.h"
#include "AD5204.h"
#include "ADC.h"
#include "TIMER.h"
#include "onewire.h"

#define cbi(reg,bit)	reg &= ~(_BV(bit))
#define sbi(reg,bit)	reg |= (_BV(bit))

volatile unsigned char out_buf[8] = {'Q',0,0,0,0,0,0,0};
volatile unsigned char *out_vec;

int main(void)
{
	DDRC = 0;
	
	uart_init();
	AD5204_init();
	AD5204_vol(0x00);
	ADC_init();
	timer_init();

	sei();
	
	printstr_p(PSTR("ATMega88 is ready\n"));

	ow_set_bus(&PIND,&PORTD,&DDRD,PD2);

	ow_byte_wr(0xF0);
	ow_byte_rd();
	ow_byte_rd();
	ow_byte_rd();
	ow_byte_rd();
	ow_byte_rd();
	ow_byte_rd();
	ow_byte_rd();
	ow_byte_rd();
    while(1)
    {
        if(uart_flags.rxstring_end)
		{
			uart_flags.rxstring_end = 0;
			switch(rxstring[0])
			{
				case 'V':
						AD5204_vol(rxstring[1]);	
						putchr(ADC_data);
				break;
						
				case 'A':	
						putchr(ADC_data);
				break;	
				
				case 'Q':
						putchr('!');
				break;
			}
		} 
		if(ADC_flag_complete)
		{
			ADC_flag_complete = 0;
			out_buf[1] = ADC_data;
			out_buf[2] = 0x01;
			out_buf[3] = 0x01;
			out_buf[4] = 0x01;
			out_buf[5] = 0x01;
			out_buf[6] = 0x01;
			out_buf[7] = 0x01;
			printstr(out_buf);
		}	
		
		

				
    }
}